<?php

namespace app\admin\service;


use app\admin\model\DeviceAlarmType;

/**
 * 设备报警类型管理-服务类
 * Class DeviceAlarmTypeService
 * @package app\admin\service
 */
class DeviceAlarmTypeService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new DeviceAlarmType();
    }

    /**
     * 获取数据列表
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        // 初始化变量
        $map = [];
        $sort = 'sort asc,id desc';
        $is_sql = 0;

        if ($param) {
            // 筛选名称
            if (isset($param['name']) && $param['name']) {
                $map[] = ['name', 'like', "%{$param['name']}%"];
            }
        }


        //获取数据总数
        $count = $this->model->where($map)->count();
        // 打印SQL
        if ($is_sql) {
            echo $this->model->getLastSql();
        }
        $list = [];
        if ($count > 0) {
            $list = $this->model
                ->where($map)
                ->order($sort)
                ->page(PAGE, PERPAGE)
                ->field('update_time,delete_time', true)
                ->select();
        }

        //返回结果
        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
    }


    public function delete()
    {
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return message("记录ID不能为空", false);
        }
        $ids = (array)$ids;

        $result = DeviceAlarmType::destroy($ids);
        if (!$result) {
            return message("删除失败", false);
        }
        return message("删除成功");
    }


}

